package com.javasm.controller.MisMannageController;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import com.javasm.entity.MisMannageEntity.ProdInfo;
import com.javasm.entity.MisMannageEntity.ReturnCode;
import com.javasm.entity.MisMannageEntity.ReturnEntity;
import com.javasm.service.MisMannageService.ProductListService;
import com.javasm.service.MisMannageService.impl.ProductListServiceImpl;

@WebServlet("/prodlist/*")
public class ProductListServlet extends BaseServlet {

    public void get(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String seriesName = req.getParameter("seriesName");

        ProductListService productListService = new ProductListServiceImpl();
        List<ProdInfo> prodInfos = productListService.getProdlist(seriesName);

        ReturnEntity returnEntity = new ReturnEntity();
        if (prodInfos != null) {
            returnEntity.setRetCode(ReturnCode.QUERY_OK.getCode());
            returnEntity.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            returnEntity.setRetData(prodInfos);
        } else {
            returnEntity.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            returnEntity.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSON(returnEntity));
        System.out.println(JSON.toJSON(returnEntity));
        writer.flush();
        writer.close();
    }
}
